﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.Catering_Page.yl.Daily
{
    public partial class EquipmentSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddl_type.SelectedItem.Text = "全部";               
                Rep_Plan.DataSource = null;
                Rep_Plan.DataBind();                
                LoadDrop();
                LoadForm();
            }
        }
        protected void LoadDrop()
        {
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                Bind_Data_to_Controls.Bind_Area_ddl(ddl_group, isAdmin, groupid, companyid, true, false);
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_group.SelectedItem, ddl_Class, isAdmin, classid, true);
            }
        }
        protected void ddl_group_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_group.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_group.SelectedItem, ddl_Class);
            }
        }

        protected void Drop_BanZu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            //this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");            
        }
        //公用的DropDownList绑定方法
        public void DropBind(DropDownList drop, DataTable dt, string strValue, string strText)
        {
            drop.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dt.Rows[i][strText].ToString();
                    item.Value = dt.Rows[i][strValue].ToString();
                    drop.Items.Add(item);
                }
            }
        }       
        public DataSet SelTJRByDateAndClassID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 RID");
            strSql.Append(" FROM DailyManagement ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet SelPNameByRID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 Name");
            strSql.Append(" FROM Personnel ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id = " + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public void LoadForm()
        {                       
            try
            {
                if( ddl_type.SelectedItem.Text == "全部")
                {
                    DataSet ds = new Catering_Rules.DMMAccount().GetList("");
                    DataTable dt_Plan = ds.Tables[0];
                    dt_Plan.Columns.Add("tempId");
                    dt_Plan.Columns.Add("ccDate");
                    dt_Plan.Columns.Add("qyDate");
                    dt_Plan.Columns.Add("ClassName");

                    DataTable dt_rowsleft = dt_Plan.Clone();
                    if (ddl_group.SelectedItem.Text == "全部")
                    {
                        for (int i = 0; i < dt_Plan.Rows.Count; i++)
                        {
                            DataRow dr = dt_rowsleft.NewRow();
                            foreach (DataColumn dc in dt_rowsleft.Columns)
                            {
                                dr[dc.ColumnName] = dt_Plan.Rows[i][dc.ColumnName];
                            }
                            dt_rowsleft.Rows.Add(dr);
                        }
                        for (int i = 0; i < dt_rowsleft.Rows.Count; i++)
                        {
                            dt_rowsleft.Rows[i]["tempId"] = i + 1;
                            dt_rowsleft.Rows[i]["ccDate"] = Convert.ToDateTime(dt_rowsleft.Rows[i][5].ToString()).ToString("yyyy/MM/dd");
                            dt_rowsleft.Rows[i]["qyDate"] = Convert.ToDateTime(dt_rowsleft.Rows[i][6].ToString()).ToString("yyyy/MM/dd");
                            dt_rowsleft.Rows[i]["ClassName"] = SelCNameByCID(dt_rowsleft.Rows[i]["CLassID"].ToString()).Tables[0].Rows[0][0];
                        }
                    }
                    else if (ddl_group.SelectedItem.Text != "全部" && ddl_Class.SelectedItem.Text == "全部")
                    {
                        for (int i = 0; i < dt_Plan.Rows.Count; i++)
                        {
                            DataSet ds_areaid = SelClassInfoAndClassID(dt_Plan.Rows[i]["ClassID"].ToString());
                            DataTable dt_areaid = ds_areaid.Tables[0];
                            if (dt_areaid.Rows[0]["AreaID"].ToString() == ddl_group.SelectedValue)
                            {
                                DataRow dr = dt_rowsleft.NewRow();
                                foreach (DataColumn dc in dt_rowsleft.Columns)
                                {
                                    dr[dc.ColumnName] = dt_Plan.Rows[i][dc.ColumnName];
                                }
                                dt_rowsleft.Rows.Add(dr);
                            }
                        }
                        for (int i = 0; i < dt_rowsleft.Rows.Count; i++)
                        {
                            dt_rowsleft.Rows[i]["tempId"] = i + 1;
                            dt_rowsleft.Rows[i]["ccDate"] = Convert.ToDateTime(dt_rowsleft.Rows[i][5].ToString()).ToString("yyyy/MM/dd");
                            dt_rowsleft.Rows[i]["qyDate"] = Convert.ToDateTime(dt_rowsleft.Rows[i][6].ToString()).ToString("yyyy/MM/dd");
                            dt_rowsleft.Rows[i]["ClassName"] = SelCNameByCID(dt_rowsleft.Rows[i]["CLassID"].ToString()).Tables[0].Rows[0][0];
                        }
                    }
                    else if (ddl_group.SelectedItem.Text != "全部" && ddl_Class.SelectedItem.Text != "全部")
                    {
                        for (int i = 0; i < dt_Plan.Rows.Count; i++)
                        {
                            if (dt_Plan.Rows[i]["ClassID"].ToString() == ddl_Class.SelectedValue)
                            {
                                DataRow dr = dt_rowsleft.NewRow();
                                foreach (DataColumn dc in dt_rowsleft.Columns)
                                {
                                    dr[dc.ColumnName] = dt_Plan.Rows[i][dc.ColumnName];
                                }
                                dt_rowsleft.Rows.Add(dr);
                            }
                        }
                        for (int i = 0; i < dt_rowsleft.Rows.Count; i++)
                        {
                            dt_rowsleft.Rows[i]["tempId"] = i + 1;
                            dt_rowsleft.Rows[i]["ccDate"] = Convert.ToDateTime(dt_rowsleft.Rows[i][5].ToString()).ToString("yyyy/MM/dd");
                            dt_rowsleft.Rows[i]["qyDate"] = Convert.ToDateTime(dt_rowsleft.Rows[i][6].ToString()).ToString("yyyy/MM/dd");
                            dt_rowsleft.Rows[i]["ClassName"] = SelCNameByCID(dt_rowsleft.Rows[i]["CLassID"].ToString()).Tables[0].Rows[0][0];
                        }
                    }
                    this.Rep_Plan.DataSource = dt_rowsleft;
                    this.Rep_Plan.DataBind();                
                }
                else
                {
                    DataSet ds = new Catering_Rules.DMMAccount().GetList(" PName like '%" + txt_Name.Text + "%'");
                    DataTable dt_Plan = ds.Tables[0];
                    dt_Plan.Columns.Add("tempId");
                    dt_Plan.Columns.Add("ccDate");
                    dt_Plan.Columns.Add("qyDate");
                    dt_Plan.Columns.Add("ClassName");
                    for (int i = 0; i < dt_Plan.Rows.Count; i++)
                    {
                        dt_Plan.Rows[i]["tempId"] = i + 1;
                        dt_Plan.Rows[i]["ccDate"] = Convert.ToDateTime(dt_Plan.Rows[i][5].ToString()).ToString("yyyy/MM/dd");
                        dt_Plan.Rows[i]["qyDate"] = Convert.ToDateTime(dt_Plan.Rows[i][6].ToString()).ToString("yyyy/MM/dd");
                        dt_Plan.Rows[i]["ClassName"] = SelCNameByCID(dt_Plan.Rows[i]["CLassID"].ToString()).Tables[0].Rows[0][0];
                    }
                    this.Rep_Plan.DataSource = dt_Plan;
                    this.Rep_Plan.DataBind();   
                }
            }
            catch
            {

            }
        }
        public DataSet SelCNameByCID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 CName");
            strSql.Append(" FROM ClassInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id = " + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet SelClassInfoAndClassID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 CName,AreaID");
            strSql.Append(" FROM ClassInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id=" + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        protected void Search_Click(object sender, EventArgs e)
        {
            LoadForm();
        }

        protected void ddl_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_type.SelectedItem.Text == "全部")
            {
                ddl_Class.Enabled = true;
                ddl_group.Enabled = true;
                txt_Name.Enabled = false;
            }

            if (ddl_type.SelectedItem.Text == "设备名称")
            {
                txt_Name.Enabled = true;
                ddl_Class.Enabled = false;
                ddl_group.Enabled = false;
            }               
        }         
    }
}